<?php

namespace App\Models;


class WxPay extends BaseModel
{

    public function setConfig($config)
    {
        $this->config = $config;
    }

    /**
     * 预支付请求接口(POST)
     * 返回json的数据
     */
    public function prepay($payArr, $type = 0)
    {
        $payType = ['JSAPI', 'NATIVE', 'APP'];
        $total_fee = $payArr['total_fee'] * 100;
        if (isset($GLOBALS['admin_id']) && in_array($GLOBALS['admin_id'], [1, 10])) {
            $total_fee = 1;
        }
        $unifiedorder = [
            'appid' => $this->config['appid'],
            'mch_id' => $this->config['pay_mchid'],
            'nonce_str' => $this->getNonceStr(),
            'body' => $payArr['body'],
            'out_trade_no' => $payArr['order_sn'],
            'total_fee' => $total_fee,
            'spbill_create_ip' => $_SERVER['REMOTE_ADDR'],
            'notify_url' => $this->config['notify_url'],
            'trade_type' => $payType[$type],
            'openid' => isset($payArr['openid']) ? $payArr['openid'] : ''
        ];
//        tolog('unifiedorder');
//        tolog($unifiedorder);
        $unifiedorder['sign'] = $this->makeSign($unifiedorder);
        //请求数据
        $xmldata = $this->array2xml($unifiedorder);
        $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
        $res = $this->curl_post_ssl($url, $xmldata);
        if (!$res) {
//            $this->return_err("无法连接微信服务器");
            $this->error = "无法连接微信服务器";
            return false;
        }
        $content = $this->xml2array($res);
        if (strval($content['return_code']) == 'FAIL') {
//            $this->return_err(strval($content['return_msg']));
            $this->error = strval($content['return_msg']);
            return false;
        }
        if (strval($content['result_code']) == 'FAIL') {
//            $this->return_err(strval($content['err_code_des']));
            $this->error = strval($content['err_code_des']);
            return false;
        }
        if ($type == 1) {
            return $content['code_url'];
        }
        //拼接小程序的接口数据
        $resData = [
            'appId' => strval($content['appid']),
            'timeStamp' => strval(time()),
            'nonceStr' => $this->getNonceStr(),
            'package' => 'prepay_id=' . strval($content['prepay_id']),
            'signType' => 'MD5',
        ];
        //加密签名
        $resData['paySign'] = $this->makeSign($resData);
        return $resData;
    }


    /**
     * @return array|bool
     * 微信支付回调验证
     * 返回数据
     * $type:1零售支付  2.购买水票  3.店铺续费
     */
    public function notify($type = 1)
    {
//        $xml = $GLOBALS['HTTP_RAW_POST_DATA'];
        $xml = file_get_contents('php://input');
        //将服务器返回的XML数据转化为数组
        $data = $this->xml2array($xml);
        tolog('notify-data');
        tolog($data);
//        $wechatSet = new WechatSet();
//        $wechatSet = $wechatSet->_getOne(['mch_id' => $data['mch_id']]);
//        $this->setConfig(['pay_apikey' => $wechatSet->mch_key]);
//        // 保存微信服务器返回的签名sign
//        $data_sign = $data['sign'];
//        // sign不参与签名算法
//        unset($data['sign']);
//        $sign = $this->makeSign($data);
//        // 判断签名是否正确  判断支付状态
//        if (($sign === $data_sign) && ($data['return_code'] == 'SUCCESS') && ($data['result_code'] == 'SUCCESS')) {
            $result = $data;
//            //获取服务器返回的数据
//            //更新数据库
//        } else {
//            $result = false;
//        }
//        // 返回状态给微信服务器
//        if ($result) {
            $dealResult = false;
            $model = '';
            if ($type == 1) {
                $model = new Order();
            }
            if ($type == 2) {
                $model = new WaterBillOrder();
            }
            if ($type == 3) {
                $model = new ServiceCharge();
            }
            if ($model) {
                $dealResult = $model->dealOrder($result['out_trade_no']);
                tolog('dealResult: '.($dealResult ? 'yes' : 'no'));
                tolog('error: '.$model->getError());
            }
            if ($dealResult) {
                $str = '<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>';
            } else {
                if ($model && $model->getError()) {
                    $notifyLog = new NotifyLog();
                    $notifyLog->add($type, $result, $model->getError());
                    $model->dealOrder($result['out_trade_no']);
                }
            }
//            return view('admin.index');
//        } else {
//            $str = '<xml><return_code><![CDATA[FAIL]]></return_code><return_msg><![CDATA[签名失败]]></return_msg></xml>';
//        }
//        echo $str;
//        return $result;
    }

    /**
     * 错误返回提示
     * @param string $errMsg 错误信息
     * @param string $status 错误码
     * @return  json的数据
     */
    protected function return_err($errMsg = 'error', $status = 0)
    {
        exit(json_encode(['status' => $status, 'data' => 'fail', 'msg' => $errMsg]));
    }


    /**
     * 正确返回
     * @param  array $data 要返回的数组
     * @return  json的数据
     */
    protected function return_data($data = [])
    {
        exit(json_encode(['status' => 1, 'data' => $data, 'msg' => 'success']));
    }

    /**
     * 将一个数组转换为 XML 结构的字符串
     * @param array $arr 要转换的数组
     * @param int $level 节点层级, 1 为 Root.
     * @return string XML 结构的字符串
     */
    protected function array2xml($arr, $level = 1)
    {
        $s = $level == 1 ? "<xml>" : '';
        foreach ($arr as $tagname => $value) {
            if (is_numeric($tagname)) {
                $tagname = $value['TagName'];
                unset($value['TagName']);
            }
            if (!is_array($value)) {
                $s .= "<{$tagname}>" . (!is_numeric($value) ? '<![CDATA[' : '') . $value . (!is_numeric($value) ? ']]>' : '') . "</{$tagname}>";
            } else {
                $s .= "<{$tagname}>" . $this->array2xml($value, $level + 1) . "</{$tagname}>";
            }
        }
        $s = preg_replace("/([\x01-\x08\x0b-\x0c\x0e-\x1f])+/", ' ', $s);
        return $level == 1 ? $s . "</xml>" : $s;
    }

    /**
     * 将xml转为array
     * @param  string $xml xml字符串
     * @return array    转换得到的数组
     */
    protected function xml2array($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $result = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $result;
    }

    /**
     *
     * 产生随机字符串，不长于32位
     * @param int $length
     * @return 产生的随机字符串
     */
    protected function getNonceStr($length = 32)
    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }

    /**
     * 生成签名
     * @return 签名
     */
    protected function makeSign($data)
    {
        //获取微信支付秘钥
        $key = $this->config['pay_apikey'];
        //去空
        $data = array_filter($data);
        //签名步骤一：按字典序排序参数
        ksort($data);
        $string_a = http_build_query($data);
        $string_a = urldecode($string_a);
        //签名步骤二：在string后加入KEY
        $string_sign_temp = $string_a . "&key=" . $key;
        //签名步骤三：MD5加密
        $sign = md5($string_sign_temp);
        // 签名步骤四：所有字符转为大写
        $result = strtoupper($sign);
        return $result;
    }

    /**
     * 微信支付发起请求
     */
    protected function curl_post_ssl($url, $xmldata, $second = 30, $aHeader = [])
    {
        $ch = curl_init();
        //超时时间
        curl_setopt($ch, CURLOPT_TIMEOUT, $second);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        //这里设置代理，如果有的话
        //curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
        //curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        if (count($aHeader) >= 1) {
            curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmldata);
        $data = curl_exec($ch);
        if ($data) {
            curl_close($ch);
            return $data;
        } else {
            $error = curl_errno($ch);
            echo "call faild, errorCode:$error\n";
            curl_close($ch);
            return false;
        }
    }

    //$type : 1.零售 2.水票
    public function getPayArr($xcx_openid, $wx_openid, $wechatSet, $order, $type = 1)
    {
        $notify_url = urlHome('order', 'anyPayNotify');
        $name = '订单';
        if ($type == 2) {
            $notify_url = urlHome('waterBill', 'anyPayNotify');
            $name = '水票';
        }
        $config = [
            'pay_mchid' => $wechatSet['mch_id'],
            'pay_apikey' => $wechatSet['mch_key'],
            'notify_url' => $notify_url,
            'body' => $name . '编号:' . $order['order_sn']
        ];
        if ($xcx_openid) {
            $config['appid'] = $wechatSet['xcx_app_id'];
        } else {
            $config['appid'] = $wechatSet['wx_app_id'];
        }
        if (!$config['appid']) {
            return false;
        }
        $this->setConfig($config);

        $openid = $xcx_openid ? $xcx_openid : $wx_openid;
        $payArr['openid'] = $openid;
        $payArr['body'] = $config['body'];
        $payArr['order_sn'] = $order['order_sn'];
        $payArr['total_fee'] = $order['order_amount'];

        return $payArr;
    }
}
